const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const User = sequelize.define('User', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  username: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
    comment: '用户名'
  },
  password: {
    type: DataTypes.STRING(255),
    allowNull: true,
    comment: '密码'
  },
  email: {
    type: DataTypes.STRING(100),
    allowNull: true,
    unique: true,
    comment: '邮箱'
  },
  lesson_fee: {
    type: DataTypes.DECIMAL(10, 2),
    defaultValue: 0,
    comment: '课时费'
  },
  nickname: {
    type: DataTypes.STRING(50),
    allowNull: true,
    comment: '昵称'
  },
  avatar: {
    type: DataTypes.STRING(255),
    allowNull: true,
    comment: '头像'
  },
  gender: {
    type: DataTypes.ENUM('male', 'female', 'unknown'),
    defaultValue: 'unknown',
    comment: '性别'
  },
  birthday: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '生日'
  },
  role: {
    type: DataTypes.ENUM('student', 'teacher', 'admin', 'super_admin'),
    defaultValue: 'student',
    comment: '角色'
  },
  status: {
    type: DataTypes.ENUM('active', 'inactive', 'banned'),
    defaultValue: 'active',
    comment: '状态'
  },
  is_verified: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
    comment: '是否已验证'
  },
  last_login_at: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '最后登录时间'
  },
  last_login_ip: {
    type: DataTypes.STRING(45),
    allowNull: true,
    comment: '最后登录IP'
  },
  sign: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '签到天数'
  },
  coin: {
    type: DataTypes.DECIMAL(10, 2),
    defaultValue: 0,
    comment: '金币'
  },
  level: {
    type: DataTypes.INTEGER,
    defaultValue: 1,
    comment: '等级'
  },
  experience: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '经验值'
  },
  bio: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '个人简介'
  },
  address: {
    type: DataTypes.STRING(255),
    allowNull: true,
    comment: '地址'
  },
  school: {
    type: DataTypes.STRING(100),
    allowNull: true,
    comment: '学校'
  },
  grade: {
    type: DataTypes.STRING(20),
    allowNull: true,
    comment: '年级'
  },
  is_deleted: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
    comment: '是否已删除'
  },
  remark: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '备注'
  },
  total_lessons: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '购买的总课时数'
  },
  used_lessons: {
    type: DataTypes.DECIMAL(10, 2),
    defaultValue: 0,
    comment: '已使用的课时数（支持小数）'
  },
  created_at: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
    comment: '创建时间'
  },
  updated_at: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
    comment: '更新时间'
  }
}, {
  tableName: 'users',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  indexes: [
    {
      fields: ['username']
    },
    {
      fields: ['email']
    },
    {
      fields: ['lesson_fee']
    },
    {
      fields: ['role']
    },
    {
      fields: ['status']
    }
  ]
});

module.exports = User; 